package cn.changein.framework.modular.exportSetting.service.impl;

import cn.changein.framework.base.pojo.page.PageFactory;
import cn.changein.framework.base.pojo.page.PageInfo;
import cn.changein.framework.core.util.spring.SpringUtil;
import cn.changein.framework.kernel.model.response.ResponseData;
import cn.changein.framework.modular.exportSetting.entity.ExportSetting;
import cn.changein.framework.modular.exportSetting.mapper.ExportSettingMapper;
import cn.changein.framework.modular.exportSetting.model.params.ExportSettingParam;
import cn.changein.framework.modular.exportSetting.model.result.ExportSettingResult;
import cn.changein.framework.modular.exportSetting.service.ExportSettingService;
import cn.changein.framework.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * <p>
 * 导出设置 服务实现类
 * </p>
 *

 * @Date 2020-11-28
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
@Service
public class ExportSettingServiceImpl extends ServiceImpl<ExportSettingMapper, ExportSetting> implements ExportSettingService {

    @Override
    public void add(ExportSettingParam param){
        ExportSetting entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ExportSettingParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ExportSettingParam param){
        ExportSetting oldEntity = getOldEntity(param);
        ExportSetting newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ExportSettingResult findBySpec(ExportSettingParam param){
        return null;
    }

    @Override
    public List<ExportSettingResult> findListBySpec(ExportSettingParam param){
        return null;
    }

    @Override
    public PageInfo findPageBySpec(ExportSettingParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return PageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData getExportData(JSONObject jsonParam) {
        String exportCode = jsonParam.getString("settingCode");
        JSONObject param = jsonParam.getJSONObject("param");
        ExportSettingResult exportSettingByCode = this.baseMapper.getExportSettingByCode(exportCode);
        if (exportSettingByCode!=null){
           return reflectionExportDataBySettingInfo(exportSettingByCode,param);
        }
        return ResponseData.error("导出信息为配置，请前往设置-开发设置-导出管理配置导出信息");
    }

    @Override
    public ResponseData validateExportSetting(ExportSettingParam exportSettingParam) {
        String exportCode = exportSettingParam.getExportCode();
        if (!StringUtils.isEmpty(exportCode)) {
            List<ExportSettingResult> exportSettingResult = this.baseMapper.validateExportSetting(exportSettingParam);
            if (exportSettingResult.size()>0){
                return ResponseData.error("已存在编号为"+exportCode+"的导出设置！");
            }else {
                return ResponseData.success();
            }
        }
        return ResponseData.error("导出设置不存在！");
    }

    /*

     * @Description //TODO 反射调用对应导出配置的查询方法
     * @Date 2020-11-30 13:54
     * @MethodName reflectionExportDataBySettingInfo
     * @Param [exportSettingByCode, param]
     * @return cn.changein.framework.kernel.model.response.ResponseData
     **/
    private ResponseData reflectionExportDataBySettingInfo(ExportSettingResult exportSettingByCode, JSONObject param) {
        String exportMethodName = exportSettingByCode.getExportMethodName();//方法名
        String exportClassName = exportSettingByCode.getExportClassName();//导出类名
        String paramClassName = exportSettingByCode.getParamClassName();//导出参数类路径
        try {
            ApplicationContext applicationContext = SpringUtil.getApplicationContext();

            Class<?> exportMethodClass = Class.forName(exportClassName);
            Class<?> paramClass = Class.forName(paramClassName);
            Object paramInfo = JSONObject.toJavaObject(param, paramClass);
            //获取具体导出方法的对象
            Object exportObject =applicationContext.getBean(exportMethodClass);
            //获取对应的方法
            Method declaredMethod = exportMethodClass.getDeclaredMethod(exportMethodName, paramClass);
            ResponseData responseData = (ResponseData) declaredMethod.invoke(exportObject,paramInfo);
            return responseData;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return ResponseData.error("导出异常！");
    }

    private Serializable getKey(ExportSettingParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private ExportSetting getOldEntity(ExportSettingParam param) {
        return this.getById(getKey(param));
    }

    private ExportSetting getEntity(ExportSettingParam param) {
        ExportSetting entity = new ExportSetting();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
